package com.gua.jbs.daoImpl;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gua.jbs.dao.ICustomerDao;
import com.gua.jbs.pojo.Jbscit;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.QueryConstant;
import com.gua.jbs.utility.StringUtil;

public class CustomerDao extends BaseDao implements ICustomerDao {

	public String saveCustomer(Jbscit jbscit) throws ApplicationException {
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isSaved = false;
		int pk = 0;
		if (jbscit != null) {
			try {
				pk = getPrimaryKey("CUST_ID", "JBSCIT");
				// set the parameters
				lParamList.add(0, pk);
				lParamList.add(1, jbscit.getCustFName());
				lParamList.add(2, jbscit.getCustLName());
				lParamList.add(3, jbscit.getCustAddress());
				lParamList.add(4, jbscit.getCustExtraAddr());
				lParamList.add(5, jbscit.getCustLandmark());
				if(StringUtil.isNullOrEmpty(jbscit.getCustMobile()))
					lParamList.add(6, jbscit.getCustMobile());
				else
					lParamList.add(6, null);
				if(StringUtil.isNullOrEmpty(jbscit.getCustHomePhone()))
					lParamList.add(7, jbscit.getCustHomePhone());
				else
					lParamList.add(7, null);
				lParamList.add(8, jbscit.getCustEmailAddr());
				lParamList.add(9, 1);
				lParamList.add(10, 1);
				lParamList.add(11, 1);
				lParamList.add(12, DBConstant.IS_ACTIVE_YES);
				lParamList.add(13, new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(14, new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(15, jbscit.getExtrafield1());
				lParamList.add(16, null);
				lParamList.add(17, null);
				lParamList.add(18, "Yogesh");
				lParamList.add(19, "Yogesh");
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.SAVE_CUST_BILL,
						lParamList);
				int resultCount = insert(query);
				if (resultCount != 0)
					isSaved = true;
			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.CUSTOMER_FOR_BILL_NOT_SAVED);
			}

		}
		if (isSaved)
			return pk + "";
		else
			return null;

	}
}
